package cn.movie.admin.controller;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import cn.movie.admin.service.movieService;
import cn.util.Page;
import cn.util.WiselySettings;

@Controller
@RequestMapping(value="/admin/movie")
public class movieController {
	@Autowired
	movieService movieservice;
	@Autowired
	WiselySettings wiselySettings;
	Page page;
	private static SimpleDateFormat sdf = new SimpleDateFormat(
			"yyyyMMddhhmmssSSS"); 
	
	@RequestMapping(value="/movieTypeList")
	public ModelAndView movieTypeList(HttpServletRequest request){
		Map<String,Object> map=new HashMap<String,Object>();
		Map<String, Object> selUser=new HashMap<String, Object>();
		String movieTypeName=request.getParameter("movieTypeName");
		selUser.put("movieTypeName", movieTypeName);
		int pageNow=1;
		String yehao=request.getParameter("yehao");
		if(yehao!=null){
			pageNow=Integer.parseInt(yehao);
		}
		if(pageNow<=0){
			pageNow=1;
		}
		
		int pageSize=10;
		try {  
            pageSize = Integer.parseInt(request.getParameter("pagesize"));  
        } catch (Exception e) {  
          
        }  
		
		long totalCount=movieservice.selCountMovieType(selUser);//得到总条数
		//取得总页数
		int zong=(int)(totalCount / pageSize )+(totalCount % pageSize > 0 ? 1: 0);
		if(pageNow>zong){
			pageNow=zong;
		}
		page=new Page(totalCount, pageNow, pageSize);
		selUser.put("start", page.getStartPos());
		selUser.put("pageSize", pageSize);
		
		map.put("page", page);
		map.put("zong", zong);
		List list=movieservice.selMovieTypeList(selUser);
		map.put("list",list);
		map.put("movieTypeName", movieTypeName);
		return new ModelAndView("/admin/movie/movietypelist",map);
	}
	///显示添加电影类型的弹框
	@RequestMapping(value="/addMovieType")
	public String addMovieType(){
		return "/admin/movie/addMovieType";
	}
	//保存电影类型信息
	@RequestMapping("/saveMovieType")
	@ResponseBody
	public Map saveCardType(HttpServletRequest request){
		Map<String, Object> map=new HashMap<String, Object>();
		Map<String, Object> maps=new HashMap<String, Object>();
		String movieTypeName=request.getParameter("movieTypeName");
		String movieTypeDescription=request.getParameter("movieTypeDescription");
		List<Map> list=movieservice.selMovieTypeList(null);
		for(Map mm:list){
			if(movieTypeName.equals(mm.get("movieTypeName"))){
				map.put("success", false);
				map.put("msg","已有该类型名称！");
				return map;
			}
		}
		maps.put("movieTypeName", movieTypeName);
		maps.put("movieTypeDescription", movieTypeDescription);
		int row=movieservice.saveMovieType(maps);
		if(row>0){
			map.put("success", true);
			map.put("msg","保存成功！");
		}else{
			map.put("success", false);
			map.put("msg","保存失败！");
		}
		return map;
	}
	//查看电影类型的信息
	@RequestMapping(value="/selectMovieType")
	public ModelAndView selCardTypeInfo(HttpServletRequest request){
		Map<String, Object> map=new HashMap<String, Object>();
		String movieTypeId=request.getParameter("movieTypeId");
		map.put("movieType", movieservice.selMovieTypeInfo(movieTypeId));
		
		return new ModelAndView("/admin/movie/updateMovieType",map);
	}
	@RequestMapping(value="/editMovieType")
	@ResponseBody
	public Map editCardType(@RequestParam Map<String,Object> param){
		Map<String, Object> map=new HashMap<String, Object>();
		int row=movieservice.editMovieType(param);
		if(row>0){
			map.put("success", true);
			map.put("msg","修改成功！");
		}else{
			map.put("success", false);
			map.put("msg","修改失败！");
		}
		return map;
		
	}
	@RequestMapping(value="/deleteMovieType")
	@ResponseBody
	public Map deleteMovieType(String movieTypeId){
		Map<String, Object> map=new HashMap<String, Object>();
		List list=movieservice.selMovieByType(movieTypeId);
		if(list.size()!=0){
			map.put("success", false);
			map.put("msg","该类型下有电影不能删除！");
			return map;
		}
		int row = movieservice.deleteMovieType(movieTypeId);
		if(row>0){
			map.put("success", true);
			map.put("msg","删除成功！");
		}else{
			map.put("success", false);
			map.put("msg","删除失败！");
		}
		return map;
	}
	@RequestMapping(value="/movieList")
	public ModelAndView movieList(HttpServletRequest request){
		Map<String,Object> map=new HashMap<String,Object>();
		Map<String, Object> selUser=new HashMap<String, Object>();
		String movieName=request.getParameter("movieName");
		String movieDirect=request.getParameter("movieDirect");
		String movieTypeId=request.getParameter("movieTypeId");
		selUser.put("movieName", movieName);
		selUser.put("movieDirect", movieDirect);
		selUser.put("movieTypeId", movieTypeId);
		int pageNow=1;
		String yehao=request.getParameter("yehao");
		if(yehao!=null){
			pageNow=Integer.parseInt(yehao);
		}
		if(pageNow<=0){
			pageNow=1;
		}
		
		int pageSize=10;
		try {  
            pageSize = Integer.parseInt(request.getParameter("pagesize"));  
        } catch (Exception e) {  
          
        }  
		
		long totalCount=movieservice.selCountMovieList(selUser);//得到总条数
		//取得总页数
		int zong=(int)(totalCount / pageSize )+(totalCount % pageSize > 0 ? 1: 0);
		if(pageNow>zong){
			pageNow=zong;
		}
		page=new Page(totalCount, pageNow, pageSize);
		selUser.put("start", page.getStartPos());
		selUser.put("pageSize", pageSize);
		
		map.put("page", page);
		map.put("zong", zong);
		List list=movieservice.selMovieList(selUser);
		map.put("list",list);
		List tlist=movieservice.selMovieTypeList(null);
		map.put("tlist",tlist);
		map.put("movieName", movieName);
		map.put("movieDirect", movieDirect);
		map.put("movieTypeId", movieTypeId);
		return new ModelAndView("/admin/movie/list",map);
	}
	//显示添加电影弹框
	@RequestMapping(value="/addMovie")
	public ModelAndView addMovie(){
		Map<String,Object> map=new HashMap<String,Object>();
		List tlist=movieservice.selMovieTypeList(null);
		map.put("tlist",tlist);
		return new ModelAndView("/admin/movie/addMovie",map);
	}
	
	
	//保存电影信息
	@RequestMapping(value="/saveMovie")
	@ResponseBody
	public Map saveMovie(@RequestParam Map param,@RequestParam(value="picUrl",required=false) MultipartFile files){
		Map<String,Object> map=new HashMap<String,Object>();
		//上传图片
		String SiteLogo="";
		//上传的图片保存路径
		String filePath=wiselySettings.getPath()+"/moviePic/";
		File pathFile = new File(filePath);// 建文件夹
		if (!pathFile.exists()) {
			pathFile.mkdirs();
		}
		MultipartFile upFile = files;
		if(StringUtils.isEmpty(files)){
			SiteLogo="";
		}else{
			String newFileName = upFile.getOriginalFilename();//获取图片名称
			String newFilePath = filePath + newFileName;// 新路径
			File newFile = new File(newFilePath);
			if (newFile.exists()) {
				newFileName = sdf.format(new Date()) + "_"
						+ upFile.getOriginalFilename();
				newFilePath = filePath + newFileName;
				newFile = new File(newFilePath);
			}
			try {
				upFile.transferTo(newFile);
			} catch (Exception e) {
				e.printStackTrace();
			}
			SiteLogo="/upload/movie/moviePic/"+newFileName;//保存到数据库的路径
		}
		param.put("picUrl", SiteLogo);
		int row=movieservice.saveMovie(param);
		if(row>0){
			map.put("success", true);
			map.put("msg","保存成功！");
		}else{
			map.put("success", false);
			map.put("msg","保存失败！");
		}
		return map;
	}
	//显示修改电影信息的弹框
	@RequestMapping(value="/selectMovie")
	public ModelAndView selectMovie(String movieId){
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("movie",movieservice.selMovieById(movieId));
		List tlist=movieservice.selMovieTypeList(null);
		map.put("tlist",tlist);
		return new ModelAndView("/admin/movie/updateMovie",map);
	}
	//修改电影信息
	@RequestMapping(value="/editMovie")
	@ResponseBody
	public Map editMovie(@RequestParam Map param,@RequestParam(value="picUrl",required=false) MultipartFile files){
		Map<String,Object> map=new HashMap<String,Object>();
		if(!StringUtils.isEmpty(files)){
			//上传图片
			String SiteLogo="";
			//上传的图片保存路径
			String filePath=wiselySettings.getPath()+"/moviePic/";
			File pathFile = new File(filePath);// 建文件夹
			if (!pathFile.exists()) {
				pathFile.mkdirs();
			}
			MultipartFile upFile = files;
			if(StringUtils.isEmpty(files)){
				SiteLogo="";
			}else{
				String newFileName = upFile.getOriginalFilename();//获取图片名称
				String newFilePath = filePath + newFileName;// 新路径
				File newFile = new File(newFilePath);
				if (newFile.exists()) {
					newFileName = sdf.format(new Date()) + "_"
							+ upFile.getOriginalFilename();
					newFilePath = filePath + newFileName;
					newFile = new File(newFilePath);
				}
				try {
					upFile.transferTo(newFile);
				} catch (Exception e) {
					e.printStackTrace();
				}
				SiteLogo="/upload/movie/moviePic/"+newFileName;//保存到数据库的路径
			}
			param.put("picUrl", SiteLogo);
		}else{
			//如果没有修改图片
			param.put("picUrl", param.get("oldPic"));
		}
		
		int row=movieservice.editMovie(param);
		if(row>0){
			map.put("success", true);
			map.put("msg","修改成功！");
		}else{
			map.put("success", false);
			map.put("msg","修改失败！");
		}
		return map;
	}
	//删除电影信息
	@RequestMapping(value="/delete")
	@ResponseBody
	public Map delete(String movieId){
		Map<String,Object> map=new HashMap<String,Object>();
		int row=movieservice.delMovie(movieId);
		if(row>0){
			map.put("success", true);
			map.put("msg","删除成功！");
		}else{
			map.put("success", false);
			map.put("msg","删除失败！");
		}
		return map;
	}
}
